package com.htandroid.htandroid.aty;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.ab.soap.AbSoapListener;
import com.ab.soap.AbSoapParams;
import com.ab.soap.AbSoapUtil;
import com.ab.task.AbTask;
import com.ab.task.AbTaskItem;
import com.ab.task.AbTaskObjectListener;
import com.ab.util.AbDialogUtil;
import com.ab.util.AbToastUtil;
import com.ab.view.ioc.AbIocView;
import com.htandroid.htandroid.R;
import com.htandroid.htandroid.entity.GcwxList;
import com.htandroid.htandroid.excel.ExcelMaintain_bmbx;
import com.htandroid.htandroid.sys.InstorageURL;
import com.htandroid.htandroid.sys.User;
import com.htandroid.htandroid.sys.base_index;
import com.htandroid.htandroid.webservice.BuildGcwxXMLService;
import com.htandroid.htandroid.webservice.XMLGcwxVouchlist;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.onekeyshare.ShareContentCustomizeCallback;

import static com.htandroid.htandroid.constance.ConstanceServer.BaseUrl_New;
import static com.htandroid.htandroid.excel.ExcelGetDir.getExcelDir;

/**
 * Created by lkp on 2015/11/29.
 */
public class Gcwx_lr_index extends base_index {
    private ArrayList<GcwxList> vouchlist;
    String ismulpoint;//是否多点打印

    @AbIocView(id = R.id.btn1_gtv, click = "btnClick")
    TextView btn1_tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setAbContentView(R.layout.layout_gcwx_lr_index);
        initview("部门报修");
        initview2();
        getdata(0);
    }

    public void btnClick(View v) {
        gcwx_add(v);
    }

    //初始化2
    @Override
    public void initview2() {
        btn_share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                excelPath = getExcelDir() + File.separator + "部门报修.xls";
                ExcelMaintain_bmbx bmbx = new ExcelMaintain_bmbx(Gcwx_lr_index.this, excelPath, vouchlist, listtype);
                if (vouchlist.size() == 0) {
                    AbToastUtil.showToast(getApplicationContext(), "列表中没有数据，不能分享");
                } else {
                    OnekeyShare oks = new OnekeyShare();
                    oks.setTitle("部门报修.xls");
                    oks.setShareContentCustomizeCallback(new ShareContentCustomizeCallback() {
                        @Override
                        public void onShare(Platform platform,
                                            cn.sharesdk.framework.Platform.ShareParams paramsToShare) {
                            // TODO Auto-generated method stub
                            if (platform.getName().equals("Wechat")) {
                                paramsToShare.setShareType(Platform.SHARE_FILE);
                            }
                        }
                    });
                    imgPath = getExcelDir() + File.separator + "excel.png";
                    writeImg(imgPath);
                    oks.setImagePath(imgPath);
                    oks.setFilePath(excelPath);
                    oks.show(getApplicationContext());
                }
            }
        });

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //处理选状态
                linechoose("r", Integer.valueOf("" + id));
                //打开下一窗口
                Intent intent = new Intent(getApplicationContext(), Gcwx_lr_detail.class);
                //传递参数
                Bundle bundle = new Bundle();
                bundle.putSerializable("vouchlist", vouchlist);
                intent.putExtra("ctype", "edit");
                intent.putExtras(bundle);
                //从position改为id,因为过滤之后的position不正确
                intent.putExtra("currentrow", (int) id);//传递当前行,下一界面便于划动切换
                intent.putExtra("ismulpoint", ismulpoint);//是否多点打印
                startActivityForResult(intent, R.layout.layout_gcwx_lr_detail / 100000);
            }
        });
    }

    //获取数据
    @Override
    public void getdata(int page) {
        //根据下拉选择的状态显示已定价/未定价
        String djtype = "lrlist";
        AbDialogUtil.showProgressDialog(Gcwx_lr_index.this, 0, "正在加载...");
        //参数
        String urlString = "http://" + InstorageURL.SERVERURL+ BaseUrl_New;
        String[] parms = {"usercode", "cacc_id", "iyear", "djtype"};
        String[] vals = {User.uid, myApplication.getGs_ztid(), myApplication.getGs_year(), djtype};
        //生成xml
        BuildGcwxXMLService service = new BuildGcwxXMLService();
        String xmlstring = service.buildXML("gcwx_vouchlist", InstorageURL.NAME_SPACE, parms, vals, null, null, null, null, null, null, null, null);
        mAbSoapUtil.xmlCall(urlString, InstorageURL.NAME_SPACE, "gcwx_vouchlist", xmlstring, new AbSoapUtil.WebServiceCallBack() {
            @Override
            public void onSucced(String result) {
                adatperDataList.clear();
                ArrayList<Map<String, Object>> dataListForInit = new ArrayList<Map<String, Object>>();
                if (result != null) {
                    //解析xml文件,返回列表
                    vouchlist = XMLGcwxVouchlist.paraseCommentInfors(result);
                    for (int i = 0; i < vouchlist.size(); i++) {
                        //增加一个行选择状态初始化
                        sa.getIsSelected().put(i, false);
                        GcwxList wxvouch = new GcwxList();
                        wxvouch = vouchlist.get(i);
                        Map<String, Object> map = new HashMap<String, Object>();
                        map.put("index", (i + 1));
                        map.put("cvouchid", "单据号：" + wxvouch.getCvouchid());
                        map.put("cdept", "报修部门：" + wxvouch.getCdepname());
                        map.put("cnocheckreason", "维修区域：" + wxvouch.getCwxplname());
                        map.put("ddate", "报修时间：" + wxvouch.getDdate());
                        //更多数据
                        map.put("data5", "报修等级：" + wxvouch.getCdjname());
                        map.put("data6", "报修内容：" + wxvouch.getCbxtext());
                        map.put("data7", "");
                        map.put("data8", "");
                        dataListForInit.add(map);
                    }
                } else {
                    AbToastUtil.showToast(getApplicationContext(), R.string.sqlerrormsg);
                }
                toolDataProvider.initData(dataListForInit);
                loadList();
                //mHandler.sendEmptyMessage(LIST_GET_FIRST_PAGE); //加载完毕发送指令显示第一页
                //swipeRefreshLayout.setRefreshing(false);
            }

            @Override
            public void onFailure(String result) {
                //swipeRefreshLayout.setRefreshing(false);
                AbToastUtil.showToast(getApplicationContext(), R.string.sqlerrormsg);
            }
        });

        AbTask task = new AbTask();
        final AbTaskItem item = new AbTaskItem();
        item.setListener(new AbTaskObjectListener() {

            @Override
            public <T> void update(T entity) {
                String urlString = "http://" + InstorageURL.SERVERURL + BaseUrl_New;//旧版webservice地址
                AbSoapParams params = new AbSoapParams();
                params.put("arg0", myApplication.getGs_ztid());
                params.put("arg1", myApplication.getGs_year());
                params.put("arg2", "HT");
                params.put("arg3", "htgcwx_printpoint");
                mAbSoapUtil.call(urlString, InstorageURL.NAME_SPACE, "getaccinformation", params, new AbSoapListener() {
                    //获取数据成功会调用这里
                    @Override
                    public void onSuccess(int statusCode, String content) {
                        if (content != null) {
                            String res = content.split("=")[1].split(";")[0];
                            if (res.equals("anyType{}") || res.equals("")) {
                                ismulpoint = "0";
                            } else {
                                ismulpoint = res;
                            }
                        } else {
                            AbToastUtil.showToast(Gcwx_lr_index.this, R.string.sqlerrormsg);
                        }
                    }

                    // 失败，调用
                    @Override
                    public void onFailure(int statusCode, String content,
                                          Throwable error) {
                        AbToastUtil.showToast(Gcwx_lr_index.this, R.string.sqlerrormsg);
                    }

                    // 开始执行前
                    @Override
                    public void onStart() {
                        //显示进度框
                        //AbDialogUtil.showProgressDialog(Gcwx_lr_index.this, 0, "正在加载...");
                    }

                    // 完成后调用，失败，成功
                    @Override
                    public void onFinish() {
                        //移除进度框
                        AbDialogUtil.removeDialog(Gcwx_lr_index.this);
                    }
                });
            }

            @SuppressWarnings("unchecked")
            @Override
            public Integer getObject() {
                return -1;
            }
        });
        task.execute(item);

        //获取是否存在多点打印参数
//        TreeMap<String, String> properties = new TreeMap<String, String>();
//        properties.put("arg0", myApplication.getGs_ztid());
//        properties.put("arg1", myApplication.getGs_year());
//        properties.put("arg2", "HT");//csysid
//        properties.put("arg3", "htgcwx_printpoint");//cname
//        ToolSOAP.callService(InstorageURL.SERVERURL, InstorageURL.NAME_SPACE, "getaccinformation", properties, new ToolSOAP.WebServiceCallBack() {
//            @Override
//            public void onSucced(SoapObject result) {
//                loadingDialog.loadingfinish();
//                if (result != null) {
//                    String res = result.getProperty(0).toString();
//                    if (res.equals("anyType{}") || res.equals("")) {
//                        ismulpoint = "0";
//                    } else {
//                        ismulpoint = res;
//                    }
//                } else {
//                    AbToastUtil.showToast(getApplicationContext(), R.string.sqlerrormsg);
//                }
//            }
//
//            @Override
//            public void onFailure(String result) {
//                AbToastUtil.showToast(getApplicationContext(), R.string.sqlerrormsg);
//            }
//        });
    }

    //新增
    public void gcwx_add(View v) {
        //获取对象
        Intent intent = new Intent(getApplicationContext(), Gcwx_lr_detail.class);// 创建Intent对象
        Bundle bundle = new Bundle();
        bundle.putSerializable("vouchlist", vouchlist);
        intent.putExtra("ctype", "add");
        intent.putExtras(bundle);
        intent.putExtra("currentrow", vouchlist.size() + 1);
        intent.putExtra("ismulpoint", ismulpoint);//是否多点打印
        startActivityForResult(intent, R.layout.layout_gcwx_lr_detail / 100000);
    }

    //完成后的返回
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (resultCode) {
            case R.layout.layout_gcwx_lr_detail / 100000:
                getdata(0);
                etSearch.getText().clear();
                break;
            case R.layout.basenvo_nofunc / 300000:
                //处理选状态
                linechoose("r", data.getExtras().getInt("currentrow"));
                break;
            default:
                //其它窗口的回传数据
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
